package com.ja.openchat.ui.login;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentPhoneCodeBinding;
import com.ja.openchat.viewmodels.PhoneCodeViewModel;
import com.zy.openchat.util.KeyboardHelper;

import androidx.lifecycle.Observer;
import me.goldze.mvvmhabit.base.BaseFragment;

//输入手机验证码界面
public class PhoneCodeFragment extends BaseFragment<FragmentPhoneCodeBinding, PhoneCodeViewModel> {

    private Bundle arguments;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_phone_code;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        arguments = getArguments();
    }

    @Override
    public void initView() {
        super.initView();
        if (binding.verPhoneCode.getEdit() != null) {
            KeyboardHelper.openKeyBoard(getContext(), binding.verPhoneCode.getEdit());
        }
        binding.verPhoneCode.setOnInputFinishListener(text -> viewModel.check(text));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.timerEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                binding.tvCode.setText(s);
                if (getString(R.string.resend_verify_code).equals(s)) {
                    binding.tvCode.setEnabled(true);
                    binding.tvCode.setTextColor(getResources().getColor(R.color.black));
                } else {
                    binding.tvCode.setEnabled(false);
                    binding.tvCode.setTextColor(getResources().getColor(R.color.gray1));
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setData(arguments);
        viewModel.startCountDown();
    }
}